﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace LikeBangumi.Pages
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class SettingPage : Page
    {
        public SettingPage()
        {
            this.InitializeComponent();
        }
        private void GetSetting()
        {
            tx_Ver.Text = settings.GetVersion();
            if (settings.SettingContains("Bili-Type"))
            {
                if (settings.GetSettingValue("Bili-Type").ToString() == "mp4")
                {
                    rb_mp4.IsChecked = true;
                }
                else
                {
                    rb_flv.IsChecked = true;
                }
            }
            else
            {
                rb_mp4.IsChecked = true;
            }

            if (settings.SettingContains("Bili-Qu"))
            {
                if ((int)settings.GetSettingValue("Bili-Qu") == 1)
                {
                    cb_Qx.SelectedIndex = 0;
                }
                else
                {
                    cb_Qx.SelectedIndex = 1;
                }
            }
            else
            {
                settings.SetSettingValue("Bili-Qu", 3);
            }

            if (settings.SettingContains("HideTitleBar"))
            {
                TW_HideStatus.IsOn = (bool)settings.GetSettingValue("HideTitleBar");
            }
            else
            {
                TW_HideStatus.IsOn = true;
            }

            if (settings.SettingContains("Bili-Qu"))
            {
                if ((int)settings.GetSettingValue("Bili-Qu") == 1)
                {
                    cb_Qx.SelectedIndex = 0;
                }
                else
                {
                    cb_Qx.SelectedIndex = 1;
                }
            }
            else
            {
                cb_Qx.SelectedIndex = 1;
            }

            if (settings.SettingContains("UseFFmpeg"))
            {
                sw_FFmpeg.IsOn = (bool)settings.GetSettingValue("UseFFmpeg");
            }
            else
            {
                sw_FFmpeg.IsOn = true;
            }
            string device = Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily;
            if (settings.SettingContains("AutoFull"))
            {

                sw_AutoFull.IsOn = (bool)settings.GetSettingValue("AutoFull");

            }
            else
            {
                if (device == "Windows.Mobile")
                {
                    sw_AutoFull.IsOn = true;
                }
                else
                {
                    sw_AutoFull.IsOn = false;
                }
            }

        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            GetSetting();
        }
        SettingHelper settings = new SettingHelper();
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            settings.SetSettingValue("Bili-Type", "mp4");
        }
        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            settings.SetSettingValue("Bili-Type", "flv");
        }

        private void cb_Qx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cb_Qx.SelectedIndex == 0)
            {
                settings.SetSettingValue("Bili-Qu", 1);
            }
            else
            {
                settings.SetSettingValue("Bili-Qu", 3);
            }

        }

        private void sw_FA_Toggled(object sender, RoutedEventArgs e)
        {
            if (sw_FA.IsOn)
            {
                settings.SetSettingValue("ForceAudio", true);
            }
            else
            {
                settings.SetSettingValue("ForceAudio", false);
            }
        }

        private void sw_FV_Toggled(object sender, RoutedEventArgs e)
        {
            if (sw_FV.IsOn)
            {
                settings.SetSettingValue("ForceVideo", true);
            }
            else
            {
                settings.SetSettingValue("ForceVideo", false);
            }
        }

        private void sw_FFmpeg_Toggled(object sender, RoutedEventArgs e)
        {
            if (!sw_FFmpeg.IsOn)
            {
                rb_mp4.IsChecked = true;
                settings.SetSettingValue("UseFFmpeg", false);
            }
            else
            {
                settings.SetSettingValue("UseFFmpeg", true);
            }
        }

        private void sw_AutoFull_Toggled(object sender, RoutedEventArgs e)
        {
            if (!sw_AutoFull.IsOn)
            {
                settings.SetSettingValue("AutoFull", false);
            }
            else
            {
                settings.SetSettingValue("AutoFull", true);
            }
        }

        private void TW_HideStatus_Toggled(object sender, RoutedEventArgs e)
        {
            if (TW_HideStatus.IsOn)
            {
                settings.SetSettingValue("HideTitleBar", true);
            }
            else
            {
                settings.SetSettingValue("HideTitleBar", false);
            }
        }
    }
}
